﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGenerator.BaseUI;
using CodeGenerator.Common;
using System.Collections;
using CodeGenerator.Core;
using ICSharpCode.TextEditor.Document;

namespace CodeGenerator.UI
{
    public partial class FrmTableInfo : BaseFormEx
    {

        public string tableName;
        public ServerInfo server;
        public string dbName;
        public FrmDataBaseInfo frm;

        public FrmTableInfo()
        {
            InitializeComponent();
            
        }

        private void FrmTableInfo_Load(object sender, EventArgs e)
        {
            Action action = RefreshData; // 无返回值委托
            frm.UpdateTableInfoEvent += frm_UpdateTableInfoEvent;
        }

        void frm_UpdateTableInfoEvent(ServerInfo server,string dbName, string tableName)
        {
            this.server = server;
            this.dbName = dbName;
            this.tableName = tableName;
            this.Focus();

            RefreshData();
        }

        public void RefreshData()
        {
            IDatabase dal = DBFactory.Database(this.server.DBType);

            List<ColumnInfo> list = dal.GetColumnInfoList(this.server, dbName,this.tableName);

            DataTable dt = new DataTable();
            //3.通过列架构添加列
            dt.Columns.Add(new DataColumn("Id"));
            dt.Columns.Add(new DataColumn("Name"));
            dt.Columns.Add(new DataColumn("DataType"));
            dt.Columns.Add(new DataColumn("AutoIncrement"));
            dt.Columns.Add(new DataColumn("MaxLength"));
            dt.Columns.Add(new DataColumn("DecimalNum"));
            dt.Columns.Add(new DataColumn("DefaultValue"));
            dt.Columns.Add(new DataColumn("AllowDBNull"));
            dt.Columns.Add(new DataColumn("IsPrimaryKey"));
            dt.Columns.Add(new DataColumn("Comments"));
            dt.Columns.Add(new DataColumn("NetType"));
            dt.Columns.Add(new DataColumn("NetJDBC"));
            dt.Columns.Add(new DataColumn("JavaType"));
            dt.Columns.Add(new DataColumn("JavaJDBC"));
            dt.Columns.Add(new DataColumn("IsNumeric"));
            dt.Columns.Add(new DataColumn("IsDateTime"));
            dt.Columns.Add(new DataColumn("IsBoolean"));
            dt.Columns.Add(new DataColumn("UIControlType"));

            foreach(var ColumnInfo in list)
            {
                dt.Rows.Add(ColumnInfo.Id, 
                            ColumnInfo.Name, 
                            ColumnInfo.DataType, 
                            ColumnInfo.AutoIncrement=="是"?"√":"", 
                            ColumnInfo.MaxLength,
                            ColumnInfo.DecimalNum, 
                            ColumnInfo.DefaultValue,
                            ColumnInfo.AllowDBNull == "是" ? "√" : "",
                            ColumnInfo.IsPrimaryKey == "是" ? "√" : "", 
                            ColumnInfo.Comments,
                            ColumnInfo.NetType.Alias, 
                            ColumnInfo.NetType.JdbcType,
                            ColumnInfo.JavaType.Alias,
                            ColumnInfo.JavaType.JdbcType,
                            ColumnInfo.IsNumeric ? "√" : "",
                            ColumnInfo.IsDateTime ? "√" : "",
                            ColumnInfo.IsBoolean ? "√" : "", 
                            ColumnInfo.UIControlType);//Add里面参数的数据顺序要和dt中的列的顺序对应 
            }

            dataGridView1.DataSource = dt;
         //   DataGridViewEx.AutoSizeColumn(dataGridView1);

            Hashtable ht = new Hashtable();

            TableInfo table = new TableInfo();
            table.Name = this.tableName;
            table.Comments = dal.GetTableComments(server, Constant.DatabaseName, this.tableName);
            table.ColumnList = list;
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows[e.RowIndex].Cells.Count; i++)
            {
                dataGridView1[i, e.RowIndex].ToolTipText = string.Empty;
            }
        }

        private void dataGridView1_MouseHover(object sender, EventArgs e)
        {

        }
    }
}
